package cn.com.header.zbpk.account.controller;

import cn.com.header.core.IPage;
import cn.com.header.core.ISort;
import cn.com.header.core.page.SimplePage;
import cn.com.header.core.sort.Sorter;
import cn.com.header.core.support.web.JsonViewData;
import cn.com.header.core.support.web.ResultCode;
import cn.com.header.zbpk.account.entity.User;
import cn.com.header.zbpk.account.model.UserExtend;
import cn.com.header.zbpk.account.service.IUserService;
import cn.com.header.zbpk.web.CommonController;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 用户前端控制器
 *
 * @author luohaidian
 * @date 2017-11-13
 * @time 15:35
 */
@RestController
@RequestMapping(value = "/manage/account/user", method = RequestMethod.POST)
public class UserController extends CommonController {

    @Autowired
    private IUserService iUserService;

    /**
     * 账号列表(按时间)
     * @param pageNo
     * @param enabled
     * @return
     */
    @RequestMapping(value = "/time/list")
    public JsonViewData listWithTime(
            @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(name = "keyword", required = false) String keyword,
            @RequestParam(name = "enabled") boolean enabled){
        User user = new User();
        user.setEnabled(enabled);
        user.setSchoolId(Objects.isNull(getCurrentUser().getSchoolId()) ? null : getCurrentUser().getSchoolId());
        int totalRecordCount = iUserService.loadCount(user, keyword);
        IPage page = new SimplePage(pageNo, pageSize);
        Set<ISort> sortSet = Sets.newHashSet(new Sorter("create_time", false));
        List<UserExtend> userList = totalRecordCount == 0 ? Collections.EMPTY_LIST : iUserService.loads(user, sortSet, page, keyword);
        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
        resultMap.put("totalRecordCount", totalRecordCount);
        resultMap.put("userList", userList);
        return setJsonViewData(resultMap);
    }

    /**
     * 账号列表(按角色)
     * @param pageNo 页号
     * @param roleId 角色ID
     * @param keyword 搜索关键字
     * @param enabled 账号是否有效标识
     * @return
     */
    @RequestMapping(value = "/role/list")
    public JsonViewData listWithRole(
            @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(name = "roleId", required = false) Long roleId,
            @RequestParam(name = "keyword", required = false) String keyword,
            @RequestParam(name = "enabled") boolean enabled){
        User user = new User();
        user.setEnabled(enabled);
        user.setSchoolId(getCurrentUser().getSchoolId());
        IPage page = new SimplePage(pageNo, pageSize);
        Set<ISort> sortSet = Sets.newHashSet(new Sorter("user.create_time", false));
        Map<String, Object> resultMap = iUserService.loadWithRole(user, keyword, roleId, sortSet, page);
        return setJsonViewData(resultMap);
    }

    /**
     * 账号详细信息
     * @param userId
     * @return
     */
    @RequestMapping(value = "/view")
    public JsonViewData view(Long userId){
        Optional<User> optionalUser = iUserService.loadByPK(userId,null);
        if(optionalUser.isPresent()){
            Map<String, Object> resultMap = new HashMap<>(1);
            resultMap.put("user", optionalUser.get());
            return setJsonViewData(resultMap);
        }else{
            return setJsonViewData(ResultCode.NO_EXISTS);
        }
    }
}
